<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom:10px">
      <el-input
        v-model="listQuery.communityId"
        placeholder="请输入小区编码"
        style="width: 200px;"
        class="filter-item"
      />
      <el-input
        v-model="listQuery.name"
        placeholder="请输入小区名称"
        style="width: 200px;"
        class="filter-item"
      />

      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="queryCommunity"
      >查询小区</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="addCommunity"
        v-if="list.length == 0"
      >添加小区</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" width="90">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column align="center" label="省份">
        <template slot-scope="scope">{{ scope.row.provName }}</template>
      </el-table-column>
      <el-table-column align="center" label="市、州">
        <template slot-scope="scope">{{ scope.row.cityName }}</template>
      </el-table-column>
      <el-table-column align="center" label="区、县">
        <template slot-scope="scope">{{ scope.row.areaName }}</template>
      </el-table-column>
      <el-table-column align="center" label="小区编码">
        <template slot-scope="scope">{{ scope.row.communityId }}</template>
      </el-table-column>
      <el-table-column align="center" label="小区名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="小区地址" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="right">
        <template slot-scope="{row,$index}">
          <el-button size="mini" type="danger" @click="deleteCommunity(row,$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="小区" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="小区编码" prop="type">
          <el-input v-model="temp.communityId" placeholder="请输入小区编码" />
        </el-form-item>
        <el-form-item label="小区名称" prop="type">
          <el-input v-model="temp.name" placeholder="请输入小区名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveCommunityInfo()">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="温馨提示"
      :visible.sync="deleteCommunityDailogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span>您确定删除当前小区吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteCommunityDailogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doDeleteCommunity">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCommunitys,
  getCommunitysByCondition,
  deleteCommunitys,
  saveCommunitys
} from "@/api/community";
import { parseTime } from "@/utils";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  components: {},
  data() {
    return {
      listQuery: {
        page: 1,
        row: 10,
        communityId: "",
        name: ""
      },
      list: null,
      listLoading: true,
      deleteCommunityDailogVisible: false,
      dialogFormVisible: false,
      curCommunity: {},
      temp: {
        communityId: "",
        name: ""
      },
      rules: {}
    };
  },
  watch: {
    dialogFormVisible: function(val) {
      if (val == false) {
        this.temp = {
          communityId: "",
          name: ""
        };
      }
    }
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getCommunitys().then(response => {
        this.list = response.data;
        this.listLoading = false;
      });
    },
    queryCommunity() {
      this.listLoading = true;
      getCommunitysByCondition(this.listQuery).then(response => {
        this.list = response.data;
        this.listLoading = false;
      });
    },
    addCommunity() {
      this.dialogFormVisible = true;
    },
    deleteCommunity(_row) {
      this.deleteCommunityDailogVisible = true;
      this.curCommunity = _row;
    },
    doDeleteCommunity() {
      this.listLoading = true;
      deleteCommunitys(this.curCommunity).then(response => {
        this.listLoading = false;
        this.$message({
          type: "info",
          message: response.msg
        });
        this.deleteCommunityDailogVisible = false;
        this.queryCommunity();
      });
    },
    saveCommunityInfo() {
      this.listLoading = true;
      saveCommunitys(this.temp).then(response => {
        this.listLoading = false;
        this.dialogFormVisible = false;
        this.queryCommunity();
      });
    },
    restartCommunity(_row) {}
  }
};
</script>
